//

// (  )
//

package com.tools.acceleratecore.xlib.util;

import android.text.TextUtils;
import java.io.ByteArrayOutputStream;

class Base64 {
    private static final String base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    private static final int[] base64DecodeChars = new int[]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1};

    Base64() {
    }

    public static String encode(byte[] data) {
        if (data == null) {
            return null;
        } else {
            int len = data.length;
            int i = 0;
            StringBuffer out = new StringBuffer();

            while(i < len) {
                int c1 = data[i++] & 255;
                if (i == len) {
                    out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c1 >> 2));
                    out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((c1 & 3) << 4));
                    out.append("==");
                    break;
                }

                int c2 = data[i++];
                if (i == len) {
                    out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c1 >> 2));
                    out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((c1 & 3) << 4 | (c2 & 240) >> 4));
                    out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((c2 & 15) << 2));
                    out.append("=");
                    break;
                }

                int c3 = data[i++];
                out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c1 >> 2));
                out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((c1 & 3) << 4 | (c2 & 240) >> 4));
                out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((c2 & 15) << 2 | (c3 & 192) >> 6));
                out.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(c3 & 63));
            }

            return out.toString();
        }
    }

    public static byte[] decode(String strData) {
        if (TextUtils.isEmpty(strData)) {
            return null;
        } else {
            byte[] data = strData.getBytes();
            int len = data.length;
            ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
            int i = 0;

            while(i < len) {
                int b1;
                do {
                    b1 = base64DecodeChars[data[i++]];
                } while(i < len && b1 == -1);

                if (b1 == -1) {
                    break;
                }

                int b2;
                do {
                    b2 = base64DecodeChars[data[i++]];
                } while(i < len && b2 == -1);

                if (b2 == -1) {
                    break;
                }

                buf.write(b1 << 2 | (b2 & 48) >>> 4);

                int b3;
                do {
                    b3 = data[i++];
                    if (b3 == 61) {
                        return buf.toByteArray();
                    }

                    b3 = base64DecodeChars[b3];
                } while(i < len && b3 == -1);

                if (b3 == -1) {
                    break;
                }

                buf.write((b2 & 15) << 4 | (b3 & 60) >>> 2);

                int b4;
                do {
                    b4 = data[i++];
                    if (b4 == 61) {
                        return buf.toByteArray();
                    }

                    b4 = base64DecodeChars[b4];
                } while(i < len && b4 == -1);

                if (b4 == -1) {
                    break;
                }

                buf.write((b3 & 3) << 6 | b4);
            }

            return buf.toByteArray();
        }
    }
}
